package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC994 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int orangesRotting(int[][] grid) {
            int M = grid.length;
            int N = grid[0].length;
            Queue<int[]> queue = new LinkedList<>();

            int count = 0; // count 表示新鲜橘子的数量
            for (int r = 0; r < M; r++) {
                for (int c = 0; c < N; c++) {
                    if (grid[r][c] == 1) {
                        count++;
                    } else if (grid[r][c] == 2) {
                        queue.add(new int[]{r, c});
                    }
                }
            }

            int round = 0; // round 表示腐烂的轮数，或者分钟数
            while (count > 0 && !queue.isEmpty()) {
                round++;
                int n = queue.size();
                for (int i = 0; i < n; i++) {
                    int[] orange = queue.poll();
                    int r = orange[0];
                    int c = orange[1];
                    if (r - 1 >= 0 && grid[r - 1][c] == 1) {
                        grid[r - 1][c] = 2;
                        count--;
                        queue.add(new int[]{r - 1, c});
                    }
                    if (r + 1 < M && grid[r + 1][c] == 1) {
                        grid[r + 1][c] = 2;
                        count--;
                        queue.add(new int[]{r + 1, c});
                    }
                    if (c - 1 >= 0 && grid[r][c - 1] == 1) {
                        grid[r][c - 1] = 2;
                        count--;
                        queue.add(new int[]{r, c - 1});
                    }
                    if (c + 1 < N && grid[r][c + 1] == 1) {
                        grid[r][c + 1] = 2;
                        count--;
                        queue.add(new int[]{r, c + 1});
                    }
                }
            }

            if (count > 0) {
                return -1;
            } else {
                return round;
            }
        }
    }


    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC994 an = new LC994();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
